<?php

namespace App\Console\Commands;

use App\Models\UnicomOrder;
use App\Services\UnicomService;
use Illuminate\Console\Command;

class UnicomOrderSyncCommand extends BaseCommand
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'unicomOrderSync';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '联通订单同步任务';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    protected function handleExec()
    {
        $unicomOrder = UnicomOrder::whereNotNull('phoneNum')
                            ->where('status',UnicomOrder::UNICOM_STATUS_TWO)
                            ->where('status_1',UnicomService::RES_SUCCESS)
                            ->where('status_2',UnicomService::RES_SUCCESS)
                            ->where('status_3',UnicomService::RES_SUCCESS)
                            ->where('status_4',UnicomService::RES_SUCCESS)
                            ->get();
        $unicomOrder = obToArray($unicomOrder);
        // dd($unicomOrder);
        if($unicomOrder){
            foreach($unicomOrder as $value){
                $input = array(
                    'postProvinceCode'=>$value['postProvinceCode'], //收货地址省份编码（6位）
                    'postCityCode'=>$value['postCityCode'], //收货地址地市编码（6位）
                    'postDistrictCode'=>$value['postDistrictCode'], //收货地址区县编码（6位）
                    'postAddr'=>$value['postAddr'], //收货地址详情
                    'goodsId'=>$value['goodsId'], //商品id
                    'baseProductId'=>$value['baseProductId'], //基本产品id
                    'contractId'=>$value['contractId'], //合约ID
                    'postName'=>$value['postName'], //邮递姓名
                    'postNumber'=>$value['postNumber'], //联系电话
                    'certId'=>$value['certId'], //号主身份证id
                    'certName'=>$value['certName'], //号主姓名
                    'order_no'=>$value['order_no'], //订单编号
                    'phoneNum'=>$value['phoneNum'] //选占号码
                );
                $res = UnicomService::nuicomOrderSync($input);
                // var_dump($res);die;
                if($res['rspCode'] == '0000'){
                    $update_date = array(
                        'status'=>UnicomOrder::UNICOM_STATUS_ONE,
                        'orderNo'=>$res['body']['orderNo'],
                        'orderId'=>$res['body']['orderId'],
                        'url'=>$res['body']['shortUrls'][0]['url'],
                        'update_time'=>date('Y-m-d H:i:s'),
                        'result'=>json_encode($res),
                        'remark'=>$res['rspDesc']
                    );
                    UnicomOrder::where('id',$value['id'])->update($update_date);
                    $this->infoLog('联通订单同步任务执行成功',$value);
                }else{
                    $update_date = array(
                        'update_time'=>date('Y-m-d H:i:s'),
                        'result'=>json_encode($res),
                        'remark'=>$res['rspDesc']
                    );
                    UnicomOrder::where('id',$value['id'])->update($update_date);

                    $this->infoLog('联通订单同步任务失败',$res);
                }
            }

        }
        $this->infoLog('联通订单同步任务已执行',$unicomOrder);
    }
}
